package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;

import java.math.BigDecimal;

//说明：提交校验：1.当费用明细中存在费用类型为税金时，所有费用明细行的费用类型都需要为税金，否则不能提交
//2.实付金额是否等于本次申请金额汇总金额
//使用单据：付款申请单
public class PublicReimburSubOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("expenseentryentity");//费用明细分录
        e.getFieldKeys().add("qeug_expensetype");//费用类型
        e.getFieldKeys().add("expenseamount");//本次申请金额
        e.getFieldKeys().add("qeug_amount");//实付金额
    }
    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.getValidators().add(new ValidatorExt());
    }

    class ValidatorExt extends AbstractValidator {
        @Override
        public void validate() {
            ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
            for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
                DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                BigDecimal expenseAmounts = BigDecimal.ZERO;//本次申请金额汇总
                DynamicObjectCollection expenseEntryEntity = dataEntity.getDynamicObjectCollection("expenseentryentity");//费用明细分录
                for (DynamicObject expenseEntry : expenseEntryEntity) {
                    DynamicObject expenseType = expenseEntry.getDynamicObject("qeug_expensetype");//费用类型
                    String expenseTypeNumber = expenseType.getString("number");
                    if (expenseTypeNumber.equals("FYXM0048")) {
                        //当费用明细中有存在税金的时候
                        for (int i = 0; i < expenseEntryEntity.size(); i++) {
                            DynamicObject expenseEntry1 = expenseEntryEntity.get(i);
                            DynamicObject expenseType1 = expenseEntry1.getDynamicObject("qeug_expensetype");//费用类型
                            String expenseTypeNumber1 = expenseType1.getString("number");
                            if (!expenseTypeNumber1.equals("FYXM0048")) {
                                this.addFatalErrorMessage(extendedDataEntity, "第" + (i + 1) + "行，费用明细中存在费用类型为税金，费用明细行的费用类型都需要为税金，否则不能提交");
                            }
                        }
                    }
                    BigDecimal expenseAmount = expenseEntry.getBigDecimal("expenseamount");//本次申请金额
                    expenseAmounts = expenseAmounts.add(expenseAmount);
                }
                BigDecimal qeug_amount = dataEntity.getBigDecimal("qeug_amount");//实付金额
                if (qeug_amount.compareTo(expenseAmounts) != 0) {
                    this.addFatalErrorMessage(extendedDataEntity, "实付金额不等于本次申请金额汇总金额");
                }
            }
        }
    }
}
